package com.jit.app;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


public class NavigationActivity extends Activity {

    Resources resources;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_navigation);

        resources = getResources();

        initView();

        initListener();


    }

    ViewPager navsViewPage;
    int[] pages = new int[]{R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p5};

    LinearLayout indictorsLL;

    Drawable indictorBg ;
    Drawable indictorBgSelected ;

    public void initView(){
        navsViewPage = findViewById(R.id.vp_nav);
        indictorsLL = findViewById(R.id.ll_indictors);

        //给viewPage设置适配器
        class NavPageAdapter extends PagerAdapter {
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            //返回总页数
            @Override
            public int getCount() {
                return pages.length;
            }

            //返回第position页的页面布局内容(当某一页被滑进来)
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                RelativeLayout navItem  = (RelativeLayout)LayoutInflater.from(NavigationActivity.this).inflate(R.layout.navpage_item, null);
                navItem.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                navItem.setBackgroundResource(pages[position]);
                Button button = navItem.findViewById(R.id.btn_ok);
                if(position == pages.length-1){
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            NavigationActivity.this.finish();
                            Intent intent = new Intent(NavigationActivity.this, MainActivity.class);
                            NavigationActivity.this.startActivity(intent);
                            SharedPreferences.Editor editor = NavigationActivity.this.getSharedPreferences("nav",MODE_PRIVATE).edit();
                            editor.putBoolean("isNav",true);
                            editor.commit();
                        }
                    });
                }else{
                    button.setVisibility(View.GONE);
                }
                container.addView(navItem);//页面添加到父容器
                return navItem;
            }

            //销毁第position页的页面(当某一页被滑出去)
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        };
        NavPageAdapter adapter = new NavPageAdapter();
        navsViewPage.setAdapter(adapter);
        navsViewPage.setCurrentItem(0);

        int indictorSize = resources.getDimensionPixelSize(R.dimen.indictorSize);
        int indictorMargin = resources.getDimensionPixelSize(R.dimen.indictorMargin);
        indictorBg = resources.getDrawable(R.drawable.indictor_bg);
        indictorBgSelected = resources.getDrawable(R.drawable.indictor_bg_selected);


        //初始化Indictors
        for(int i=0;i<pages.length;i++){
            View view = new View(this);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(indictorSize,indictorSize);
            layoutParams.setMarginStart(indictorMargin);
            view.setLayoutParams(layoutParams);
            if(i == 0){
                view.setBackground(indictorBgSelected);
            }else{
                view.setBackground(indictorBg);
            }
            indictorsLL.addView(view);
        }


    }


    public void initListener(){
        navsViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //页面发生切换
                for(int i=0;i<indictorsLL.getChildCount();i++){
                    indictorsLL.getChildAt(i).setBackground(indictorBg);
                }
                indictorsLL.getChildAt(position).setBackground(indictorBgSelected);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


}
